import {
  getForum,
} from "../../api/getApi"
Page({

  data: {
    benefitIndex: 0,
    typeIndex: '1',
    forumType:"1",
    forumData: [],
  },
  changeTag(e) {
    let index = e.currentTarget.dataset.data
    this.setData({
      benefitIndex: index
    })
    this.getData()
  },
  changeType(event) {
    this.setData({
      typeIndex: event.target.dataset.data
    })
  },
  getData() {
    this.setData({
      forumData: []
    })
    getForum({
      page: 1,
      limit: 99,
      type:this.data.forumType,
      isCurrent: this.data.benefitIndex == "1" ? 'true' : 'false',
      isHot: this.data.benefitIndex == "2"  ? 'true' : 'false',
      hasImage: this.data.benefitIndex == "3"  ? 'true' : 'false',
      nullImage: this.data.benefitIndex == "4" ? 'true' : 'false',
    }).then(res => {
      res.list.forEach(element => {
        if (element.image) {
          element.image = element.image.split(",")
        }
      });
      this.setData({
        forumData: res.list
      })
    })
  },
  goToForumDetail(e){
    let data = e.currentTarget.dataset.data
    wx.setStorageSync('forumDetail', JSON.stringify(data))
    wx.navigateTo({
      url: '/pages/forumDetail/forumDetail',
    })
  },
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let forumType = wx.getStorageSync('forumType')
    if(!forumType){
      forumType = "1"
    }else{
      forumType = forumType
    }
    console.log(forumType)
    this.setData({forumType:forumType})
    this.getData() 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})